<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS . DS . "MenuItem.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "MenuItem.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "MenuItem.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."Users.php";

require_once '../library/Utility/Converter.php';

class Admin_MenuItemController extends Zend_Controller_Action {

    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private $articleModel;
    private $articleRevisionModel;
    private $tagItemAssocModel;
    
    private $modelUser;
    private $currentUser;
    
    /////////////////////
    // O V E R I D E S //
    /////////////////////    
    public function init() {
        if (Zend_Auth::getInstance()->getIdentity() == FALSE) {
            $this->_redirect('admin/auth/login');
        }
        
        $layoutPath = APPLICATION_PATH . '/templates/admin/default';
        $option = array('layout' => 'index',
            'layoutPath' => $layoutPath);

        Zend_Layout::startMvc($option);
        
        
        $this->menu_item_Model = new Application_Models_Menu_Item();
        
        $this->modelUser = new End_Models_Users();
        
        //OTHER 
        $this->currentUser = $this->getInforUser();
        
        //VIEW 
        $this->view->currentUser = $this->currentUser;  
        /* Initialize action controller here */
    }

    ///////////////////
    // U T I L I E S //
    ///////////////////
    private function getInforUser() {
        try {
            $currentUser = Zend_Auth::getInstance()->getIdentity();
            return $currentUser;
        } catch (Zend_Exception $ex) {
            
        }
    }
    
    ////////////////////
    // A C T I O N S  //
    ////////////////////   
    public function indexAction() {

        $menu_items = $this->menu_item_Model->fetchAll();
        $this->view->menu_items = $menu_items;
    }

    public function addAction() {
        $form = new Application_Forms_Menu_Item();
        $form->submit->setLabel('Save');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                /* Input value */

                $menu_item_id = $form->getValue('menu_item_id');
                $item_id = $form->getValue('item_id');
                $label = $form->getValue('label');
                $link = $form->getValue('link');
                $parent_id = $form->getValue('parent_id');
                $menu_id = $form->getValue('menu_id');
                $left_id = $form->getValue('left_id');
                $right_id = $form->getValue('right_id');
                if($left_id == null)
                	$left_id = 0;
                if($right_id == null || $right_id == $left_id)
                	$right_id = 0;	
               	if($item_id == '')
               		$item_id = 0;
               	if($parent_id == '')
               		$parent_id = 0;
                	

                /* Create new menu item*/

                $menu_item = new MenuItem(
                                $menu_item_id,
                                $item_id,
                                $menu_id,
                                $label,
                                $link,
                                $left_id,
                                $right_id,
                                $parent_id);
                $inserted_id = $this->menu_item_Model->_insert($menu_item);

                // Redirect after insert.
                $param = array("menu_item_id" => $inserted_id);
                $this->_helper->redirector("edit", "menuitem", "admin", $param);
            } else {
                $form->populate($formData);
            }
        }
    }

    public function editAction() {
        $form = new Application_Forms_Menu_Item();

        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            /* User save data */

            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                /* Input value */

                $menu_item_id = $form->getValue('menu_item_id');
                $item_id = $form->getValue('item_id');
                $label = $form->getValue('label');
                $link = $form->getValue('link');
                $parent_id = $form->getValue('parent_id');
                $menu_id = $form->getValue('menu_id');
                $left_id = $form->getValue('left_id');
                $right_id = $form->getValue('right_id');

                /* Create new menu item model */

                $menu_item = new MenuItem(
                                $menu_item_id,
                                $item_id,
                                $menu_id,
                                $label,
                                $link,
                                $left_id,
                                $right_id,
                                $parent_id);

                $this->menu_item_Model->_update($menu_item);

            } else {
                $form->populate($formData);
            }
        } else {
            $menu_item_id = $this->_getParam('menu_item_id');
            if ($menu_item_id > 0) {
                $menuItemArray = Utility_Converter::objectToArray($this->menu_item_Model->_find($menu_item_id));
                $form->populate($menuItemArray);
            }
        }
    }

    public function deleteAction() {
        $menu_item_id = $this->_getParam('menu_item_id');

        $this->menu_item_Model->_delete($menu_item_id);


        $this->_helper->redirector('index');
    }
}